package com.xuexiang.xui_lib.component.progress.loading;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;
import ohos.eventhandler.EventRunner;
import ohos.media.image.PixelMap;

import com.xuexiang.xui_lib.ResourceTable;
import com.xuexiang.xui_lib.component.edittext.materialedittext.validation.TextUtils;
import com.xuexiang.xui_lib.component.textview.marqueen.WeakHandler;

public class LoadingDialog extends CommonDialog {
    /**
     * loading控件
     */
    private ARCLoadingView mLoadingView;
    /**
     * 提示文字
     */
    private Text mTvTipMessage;

    private EventRunner runner = EventRunner.current();
    private WeakHandler handler = new WeakHandler(runner);
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            hide();
        }
    };
    private long delayTime = 0;

    public LoadingDialog(Context context) {
        super(context);
        initView(context, "数据加载中...");
    }

    public LoadingDialog(Context context, String tipMessage) {
        super(context);
        initView(context, tipMessage);
    }

    private void initView(Context context, String tipMessage) {
        Component component = LayoutScatter.getInstance(context)
            .parse(ResourceTable.Layout_xui_layout_loading_view, null, true);
        mLoadingView = (ARCLoadingView) component.findComponentById(ResourceTable.Id_arc_loading_view);
        mTvTipMessage = (Text) component.findComponentById(ResourceTable.Id_tv_tip_message);
        setSize(ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
        setAlignment(LayoutAlignment.CENTER);
        setContentCustomComponent(component);
    }

    /**
     * 初始化属性
     *
     * @param context
     * @param attrs
     */
    private void initAttrs(Context context, AttrSet attrs) {
        if (attrs != null) {
            if (attrs.getAttr("lvl_message").isPresent()) {
                updateMessage(attrs.getAttr("lvl_message").get().getStringValue());
            } else {
                updateMessage("数据加载中...");
            }

            if (attrs.getAttr("lvl_icon").isPresent()) {
                setLoadingIcon(((PixelMapElement) attrs.getAttr("lvl_icon").get().getElement()).getPixelMap());
            } else {
                setLoadingIcon(ResourceTable.Media_ic_launcher);
            }
        } else {
            updateMessage("数据加载中...");
            setLoadingIcon(ResourceTable.Media_ic_launcher);
        }
    }

    public void updateMessage(String tipMessage) {
        if (mTvTipMessage != null) {
            if (!TextUtils.isEmpty(tipMessage)) {
                mTvTipMessage.setText(tipMessage);
                mTvTipMessage.setVisibility(Component.VISIBLE);
            } else {
                mTvTipMessage.setText("");
                mTvTipMessage.setVisibility(Component.HIDE);
            }
        }
    }

    /**
     * 设置loading的图标
     *
     * @param icon
     * @return LoadingDialog
     */
    public LoadingDialog setLoadingIcon(PixelMap icon) {
        if (mLoadingView != null) {
            mLoadingView.setLoadingIcon(icon);
        }
        return this;
    }

    /**
     * 设置loading的图标
     *
     * @param suorceId
     * @return LoadingDialog
     */
    public LoadingDialog setLoadingIcon(int suorceId) {
        if (mLoadingView != null) {
            mLoadingView.setLoadingIcon(suorceId);
        }
        return this;
    }

    public LoadingDialog setDelayTime(long delayTime) {
        this.delayTime = delayTime;
        return this;
    }

    @Override
    public void hide() {
        super.hide();
        mLoadingView.stop();
    }

    @Override
    public void show() {
        super.show();

        if (delayTime != 0) {
            handler.postDelayed(runnable, delayTime);
        }
        mLoadingView.start();
    }
}
